package com.yanh.seckill.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author:
 * @createTime:
 **/

/**
 * 异步定时任务不阻塞
 */
@Slf4j
@EnableAsync
//@Component
public class HelloScheduled {
     @Async
     @Scheduled(cron = "0/1 * * * * ?")
     public void hello() {
         log.error("hello...");
         try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); }

     }


    /**
     * 当前时间
     * @return
     */
    private static String startTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);

        System.out.println("当前时间："+now);
        System.out.println("min时间："+min);
        //格式化时间
        String startFormat = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("格式化时间："+startFormat);
        return startFormat;
    }
    /**
     * 结束时间
     * @return
     */
    private static String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(plus, max);

        System.out.println("当前时间："+now);
        System.out.println("加2天的时间："+plus);
        System.out.println("max时间："+max);

        //格式化时间
        String endFormat = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("格式化时间："+endFormat);
        return endFormat;
    }
    /**
     * 测试
     * @return
     */
    public static void main(String[] args) {

        startTime();
        endTime();
    }
}
